{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "c_dataset_challenge.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 2",
      "language": "python",
      "name": "python2"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "SzMETjagVah3",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import math\n",
        "import numpy as np\n",
        "import tensorflow as tf\n",
        "\n",
        "import pandas as pd"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "tnrfNWTNVah-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "c789106d-606d-4daf-b911-0519f2a2b67b"
      },
      "cell_type": "code",
      "source": [
        "\n",
        "def generate_cylinder_dataset(size):\n",
        "  base_radius = np.random.uniform(low=0.5, high=2.0, size=size)\n",
        "  height = np.random.uniform(low=0.5, high=2.0, size=size)\n",
        "  volume = math.pi * (base_radius ** 2) * height\n",
        "  return pd.DataFrame({'base_radius': base_radius, 'height': height, 'volume': volume})\n",
        "\n",
        "cylinder_train_df = generate_cylinder_dataset(500000)\n",
        "cylinder_test_df = generate_cylinder_dataset(1000)\n",
        "print cylinder_train_df.head()"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "   base_radius    height     volume\n",
            "0     1.940294  1.423212  16.832725\n",
            "1     0.624015  1.605018   1.963448\n",
            "2     1.358647  1.782694  10.338073\n",
            "3     1.627153  1.730534  14.394174\n",
            "4     1.938898  1.339364  15.818245\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Q3whX_cGVaiB",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "feature_columns = [\n",
        "  tf.feature_column.numeric_column(\"base_radius\"),\n",
        "  tf.feature_column.numeric_column(\"height\")\n",
        "]\n",
        "\n",
        "def make_input_fn(df, num_epochs, batch_size=512):\n",
        "  return tf.estimator.inputs.pandas_input_fn(\n",
        "    df,\n",
        "    df['volume'],\n",
        "    batch_size=batch_size,\n",
        "    num_epochs=num_epochs,\n",
        "    shuffle=True\n",
        "  )\n",
        "\n",
        "def predict_input_fn():\n",
        "  features = {\n",
        "    \"base_radius\": tf.constant([1.5]),\n",
        "    \"height\": tf.constant([2.0])\n",
        "  }\n",
        "  return features"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "jaiomdXRVaiD",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 709
        },
        "outputId": "0c95f295-4a08-4e29-bbc0-b3b9933232bf"
      },
      "cell_type": "code",
      "source": [
        "model = tf.estimator.DNNRegressor(\n",
        "  feature_columns=feature_columns,\n",
        "  hidden_units=[64, 32, 32, 16],\n",
        "  optimizer=tf.train.AdagradOptimizer(learning_rate=0.05)\n",
        ")\n",
        "model.train(make_input_fn(cylinder_train_df, None, 1000), max_steps=5000)\n",
        "predictions = model.predict(predict_input_fn)\n",
        "print predictions.next()\n",
        "print math.pi * (1.5 ** 2) * 2.0"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Using default config.\n",
            "WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmps7_H8Z\n",
            "INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_global_id_in_cluster': 0, '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7fc00244a1d0>, '_evaluation_master': '', '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None, '_master': '', '_device_fn': None, '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': '/tmp/tmps7_H8Z', '_train_distribute': None, '_save_summary_steps': 100}\n",
            "INFO:tensorflow:Calling model_fn.\n",
            "INFO:tensorflow:Done calling model_fn.\n",
            "INFO:tensorflow:Create CheckpointSaverHook.\n",
            "INFO:tensorflow:Graph was finalized.\n",
            "INFO:tensorflow:Running local_init_op.\n",
            "INFO:tensorflow:Done running local_init_op.\n",
            "INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmps7_H8Z/model.ckpt.\n",
            "INFO:tensorflow:loss = 82951.484, step = 1\n",
            "INFO:tensorflow:global_step/sec: 136.568\n",
            "INFO:tensorflow:loss = 916.86536, step = 101 (0.738 sec)\n",
            "INFO:tensorflow:global_step/sec: 141.678\n",
            "INFO:tensorflow:loss = 151.85858, step = 201 (0.702 sec)\n",
            "INFO:tensorflow:global_step/sec: 141.583\n",
            "INFO:tensorflow:loss = 255.61827, step = 301 (0.708 sec)\n",
            "INFO:tensorflow:global_step/sec: 136.023\n",
            "INFO:tensorflow:loss = 40.460552, step = 401 (0.733 sec)\n",
            "INFO:tensorflow:global_step/sec: 137.915\n",
            "INFO:tensorflow:loss = 114.81024, step = 501 (0.729 sec)\n",
            "INFO:tensorflow:global_step/sec: 136.425\n",
            "INFO:tensorflow:loss = 69.7533, step = 601 (0.732 sec)\n",
            "INFO:tensorflow:global_step/sec: 136.826\n",
            "INFO:tensorflow:loss = 52.200905, step = 701 (0.733 sec)\n",
            "INFO:tensorflow:global_step/sec: 133.889\n",
            "INFO:tensorflow:loss = 29.409243, step = 801 (0.747 sec)\n",
            "INFO:tensorflow:global_step/sec: 133.004\n",
            "INFO:tensorflow:loss = 14.673151, step = 901 (0.748 sec)\n",
            "INFO:tensorflow:Saving checkpoints for 1000 into /tmp/tmps7_H8Z/model.ckpt.\n",
            "INFO:tensorflow:Loss for final step: 42.821198.\n",
            "WARNING:tensorflow:Input graph does not use tf.data.Dataset or contain a QueueRunner. That means predict yields forever. This is probably a mistake.\n",
            "INFO:tensorflow:Calling model_fn.\n",
            "INFO:tensorflow:Done calling model_fn.\n",
            "INFO:tensorflow:Graph was finalized.\n",
            "INFO:tensorflow:Restoring parameters from /tmp/tmps7_H8Z/model.ckpt-1000\n",
            "INFO:tensorflow:Running local_init_op.\n",
            "INFO:tensorflow:Done running local_init_op.\n",
            "{'predictions': array([13.449084], dtype=float32)}\n",
            "14.1371669412\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "3eiffX8lVaiG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 222
        },
        "outputId": "0e90d4ba-1de7-4722-da2a-ce3b8348e091"
      },
      "cell_type": "code",
      "source": [
        "def print_rmse(model, name, df):\n",
        "  metrics = model.evaluate(input_fn = make_input_fn(cylinder_test_df, 1))\n",
        "  print('RMSE on {} dataset = {}'.format(name, np.sqrt(metrics['average_loss'])))\n",
        "print_rmse(model, 'test', cylinder_test_df)"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Calling model_fn.\n",
            "INFO:tensorflow:Done calling model_fn.\n",
            "INFO:tensorflow:Starting evaluation at 2018-08-29-16:31:21\n",
            "INFO:tensorflow:Graph was finalized.\n",
            "INFO:tensorflow:Restoring parameters from /tmp/tmps7_H8Z/model.ckpt-1000\n",
            "INFO:tensorflow:Running local_init_op.\n",
            "INFO:tensorflow:Done running local_init_op.\n",
            "INFO:tensorflow:Finished evaluation at 2018-08-29-16:31:21\n",
            "INFO:tensorflow:Saving dict for global step 1000: average_loss = 0.044495843, global_step = 1000, label/mean = 6.8507185, loss = 22.247921, prediction/mean = 6.6987953\n",
            "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1000: /tmp/tmps7_H8Z/model.ckpt-1000\n",
            "RMSE on test dataset = 0.210940375924\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "evOrmYSGVaiJ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Copyright 2018 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License"
      ]
    },
    {
      "metadata": {
        "id": "if_65Yq0heBb",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}